<?php
class ControllerProductCompare extends FrontController {
	public function index() {

		M('catalog/product');

		if (!isset($this->session->data['compare'])) {
			$this->session->data['compare'] = array();
		}

		if (isset($this->request->get['remove'])) {
			$key = array_search($this->request->get['remove'], $this->session->data['compare']);

			if ($key !== false) {
				unset($this->session->data['compare'][$key]);
			}
			$this->session->set_flashdata('success', $this->_['text_remove']);
			$this->redirect(U('product/compare'));
		}

		$this->document->setTitle($this->_['heading_title']);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->_['heading_title'],
			'href'      => U('product/compare'),
			'separator' => $this->_['text_separator']
		);

		$this->data['success'] = $this->session->flashdata('success');

		$this->data['products'] = array();

		$this->data['attribute_groups'] = array();

		foreach ($this->session->data['compare'] as $key => $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);

			if ($product_info) {

				if ($product_info['quantity'] <= 0) {
					$availability = $product_info['stock_status'];
				} elseif (C('config_stock_display')) {
					$availability = $product_info['quantity'];
				} else {
					$availability = $this->_['text_instock'];
				}

				$attribute_data = array();

				$attribute_groups = $this->model_catalog_product->getProductAttributes($product_id);

				foreach ($attribute_groups as $attribute_group) {
					foreach ($attribute_group['attribute'] as $attribute) {
						$attribute_data[$attribute['attribute_id']] = $attribute['text'];
					}
				}

				$this->data['products'][$product_id] = array(
					'product_id'   => $product_info['product_id'],
					'name'         => $product_info['name'],
					'thumb'        => thumb($product_info['image'], C('config_image_compare_width'), C('config_image_compare_height')),
					'price'        => pprintf($product_info['price'], $product_info['tax_class_id']),
					'special'      => pprintf($product_info['special'], $product_info['tax_class_id']),
					'summary'      => v5_html_entity_decode($product_info['summary']),
					'model'        => $product_info['model'],
					'manufacturer' => $product_info['manufacturer'],
					'availability' => $availability,
					'rating'       => (int)$product_info['rating'],
					'reviews'      => sprintf($this->_['text_reviews'], (int)$product_info['reviews']),
					'weight'       => $this->weight->format($product_info['weight'], $product_info['weight_class_id']),
					'length'       => $this->length->format($product_info['length'], $product_info['length_class_id']),
					'width'        => $this->length->format($product_info['width'], $product_info['length_class_id']),
					'height'       => $this->length->format($product_info['height'], $product_info['length_class_id']),
					'attribute'    => $attribute_data,
					'href'         => $product_info['link'],
					'remove'       => U('product/compare', 'remove=' . $product_id)
				);

				foreach ($attribute_groups as $attribute_group) {
					$this->data['attribute_groups'][$attribute_group['attribute_group_id']]['name'] = $attribute_group['name'];

					foreach ($attribute_group['attribute'] as $attribute) {
						$this->data['attribute_groups'][$attribute_group['attribute_group_id']]['attribute'][$attribute['attribute_id']]['name'] = $attribute['name'];
					}
				}
			} else {
				unset($this->session->data['compare'][$key]);
			}
		}

		$this->display('product/compare.tpl');
  	}

	public function add() {

		$json = array();

		if (!isset($this->session->data['compare'])) {
			$this->session->data['compare'] = array();
		}

		$product_id = (int) P('product_id', 0);

		M('catalog/product');

		$product_info = $this->model_catalog_product->getProduct($product_id);

		if ($product_info) {
			if (!in_array($product_id, $this->session->data['compare'])) {
				if (count($this->session->data['compare']) >= 4) {
					array_shift($this->session->data['compare']);
				}

				$this->session->data['compare'][] = $product_id;
			}

			$json['success'] = sprintf($this->_['text_success'], U('product/product', 'product_id=' . $product_id), v5_substr($product_info['name'], 60), U('product/compare'));
			$json['total'] = sprintf($this->_['text_compare'], (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>